package com.xingxue.logistics.controller.sys;

import com.github.pagehelper.PageInfo;
import com.xingxue.logistics.common.vo.Page;
import com.xingxue.logistics.common.vo.RestResult;
import com.xingxue.logistics.common.vo.TreeNode;
import com.xingxue.logistics.common.dto.SysMenu;
import com.xingxue.logistics.service.MenuService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * @author Kris at 2018/10/9 10:05
 */
@RestController
@RequestMapping("sys/menu")
@Api(description = "菜单相关控制器")
public class MenuController {

    @Autowired
    MenuService menuService;


    @ApiOperation(value = "获取当前登陆用户的菜单树数据")
    @GetMapping("/tree")
    public ResponseEntity<RestResult<List<TreeNode>>> tree() {
        //TODO 根据当前用户 ，只查询有权限的菜单进行加载
        List<SysMenu> menuList = menuService.loadAll();
        List<TreeNode> tree = menuService.createTree(menuList);
        return ResponseEntity.ok(new RestResult<List<TreeNode>>(true, "菜单加载完成", tree));
    }

    @ApiOperation(value = "分页查询菜单")
    @GetMapping("page")
    public ResponseEntity<RestResult<PageInfo<SysMenu>>> search(
            @RequestParam(required = false) @ApiParam(value = "查询条件：菜单ID") Integer id,
            @RequestParam(required = false) @ApiParam("查询条件：父级菜单ID") Integer parentId,
            @RequestParam(required = false) @ApiParam("查询条件：菜单名称") String menuName,
            @RequestParam(required = false, defaultValue = "1") @ApiParam(value = "当前页码", defaultValue = "1") Integer page,
            @RequestParam(required = false, defaultValue = "10") @ApiParam(value = "每页行数", defaultValue = "10") Integer rows) {
        PageInfo<SysMenu> menuPage = menuService.searchPage(id, parentId, menuName, page, rows);
        return ResponseEntity.ok(new RestResult<>(true, "数据加载完成", menuPage));
    }

    @ApiOperation(value = "加载菜单，返回树形数据，不分页")
    @GetMapping("/tree_data")
    public ResponseEntity<List<SysMenu>> treeData(
            @RequestParam(required = false) @ApiParam("查询条件：父级菜单ID") Integer parentId,
            @RequestParam(required = false) @ApiParam("查询条件：关联的角色ID") Integer roleId,
            @RequestParam(required = false) @ApiParam("查询条件：未和某个角色关联") Integer notJoinRoleId
            ) {
        List<SysMenu> menuTreeData = menuService.treeData(parentId,roleId,notJoinRoleId);
        return ResponseEntity.ok(menuTreeData);
    }


    @ApiOperation(value = "分页查询菜单(easyUI用)")
    @GetMapping("page_easyui")
    public ResponseEntity<Page<SysMenu>> searchEasyUI(@RequestParam(required = false) @ApiParam(value = "查询条件：菜单ID") Integer id,
                                                      @RequestParam(required = false) @ApiParam("查询条件：父级菜单ID") Integer parentId,
                                                      @RequestParam(required = false) @ApiParam("查询条件：菜单名称") String menuName,
                                                      @RequestParam(required = false, defaultValue = "1") @ApiParam(value = "当前页码", defaultValue = "1") Integer page,
                                                      @RequestParam(required = false, defaultValue = "10") @ApiParam(value = "每页行数", defaultValue = "10") Integer rows) {
        PageInfo<SysMenu> menuPage = menuService.searchPage(id, parentId, menuName, page, rows);
        return ResponseEntity.ok(new Page(menuPage));
    }

    @ApiOperation("新增菜单")
    @PostMapping
    public ResponseEntity<RestResult> addMenu(@RequestBody SysMenu menu) {
        RestResult result = menuService.add(menu);
        return ResponseEntity.ok(result);
    }

    @ApiOperation(value = "修改菜单", notes = "依据菜单ID进行修改")
    @PutMapping
    public ResponseEntity<RestResult> updateMenu(@RequestBody SysMenu menu) {
        RestResult result = menuService.update(menu);
        return ResponseEntity.ok(result);
    }

    @ApiOperation(value = "删除菜单", notes = "根据菜单ID删除")
    @DeleteMapping("{id}")
    public ResponseEntity<RestResult> deleteMenu(@PathVariable("id") @ApiParam("需要删除的菜单ID") Integer id) {
        RestResult result = menuService.delete(id);
        return ResponseEntity.ok(result);
    }
}
